<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ title }} - 我的应用</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.2/css/bootstrap.min.css" rel="stylesheet">
    <!-- <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.2/js/bootstrap.bundle.min.js" defer></script> -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" rel="stylesheet">
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-light bg-light mb-4">
        <div class="container">
            <a class="navbar-brand" href="{{ url_for('main.index') }}">毕业论文管理系统</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('main.index') }}">首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('main.process') }}">交付流程</a>
                    </li>
                    {% if current_user.is_authenticated %}
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('main.my_theses') }}">我的论文</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('main.submit_thesis') }}">
                            <i class="fas fa-plus"></i> 新建论文
                        </a>
                    </li>
                    {% if current_user.is_admin %}
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('admin.index') }}">管理后台</a>
                    </li>
                    {% endif %}
                    {% endif %}
                </ul>
                <ul class="navbar-nav">
                    {% if current_user.is_authenticated %}
                    <li class="nav-item">
                        <a class="nav-link position-relative" href="{{ url_for('main.messages') }}">
                            <i class="fas fa-bell"></i>
                            {% set unread_count = current_user.messages|selectattr('is_read', 'equalto', false)|list|length %}
                            {% if unread_count > 0 %}
                            <span class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-danger">
                                {{ unread_count }}
                            </span>
                            {% endif %}
                        </a>
                    </li>
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown">
                            {{ current_user.username }}
                        </a>
                        <ul class="dropdown-menu">
                            <li>
                                <a class="dropdown-item" href="{{ url_for('auth.change_password') }}">
                                    <i class="fas fa-key"></i> 修改密码
                                </a>
                            </li>
                            <li><hr class="dropdown-divider"></li>
                            <li>
                                <a class="dropdown-item" href="{{ url_for('auth.logout') }}">
                                    <i class="fas fa-sign-out-alt"></i> 退出
                                </a>
                            </li>
                        </ul>
                    </li>
                    {% else %}
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('auth.login') }}">登录</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('auth.register') }}">注册</a>
                    </li>
                    {% endif %}
                </ul>
            </div>
        </div>
    </nav>

    <div class="container">
        {% with messages = get_flashed_messages() %}
        {% if messages %}
        {% for message in messages %}
        <div class="alert alert-info">{{ message }}</div>
        {% endfor %}
        {% endif %}
        {% endwith %}

        {% block content %}{% endblock %}
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    {% block extra_scripts %}
    <script>
    function updateUnreadCount() {
        fetch("{{ url_for('main.get_unread_count') }}")
            .then(response => response.json())
            .then(data => {
                const badge = document.getElementById('unread-badge');
                if (data.count > 0) {
                    badge.textContent = data.count;
                    badge.style.display = 'inline-block';
                    
                    // 如果在消息页面，自动刷新页面内容
                    if (window.location.pathname === "{{ url_for('main.messages') }}") {
                        location.reload();
                    }
                } else {
                    badge.style.display = 'none';
                }
            });
    }

    // 页面加载时立即检查
    updateUnreadCount();

    // 每30秒检查一次新消息
    setInterval(updateUnreadCount, 30000);

    // 添加桌面通知
    function requestNotificationPermission() {
        if ("Notification" in window) {
            Notification.requestPermission().then(function(permission) {
                if (permission === "granted") {
                    console.log("通知权限已获取");
                }
            });
        }
    }

    // 显示桌面通知
    function showNotification(message) {
        if ("Notification" in window && Notification.permission === "granted") {
            new Notification("新消息提醒", {
                body: message,
                icon: "/static/favicon.ico"  // 可以添加网站图标
            });
        }
    }

    // 页面加载时请求通知权限
    document.addEventListener('DOMContentLoaded', function() {
        requestNotificationPermission();
    });

    // 添加消息轮询
    let lastCount = 0;
    function pollMessages() {
        fetch("{{ url_for('main.get_unread_count') }}")
            .then(response => response.json())
            .then(data => {
                if (data.count > lastCount) {
                    // 有新消息
                    showNotification("您有新的消息，请查看");
                    // 播放提示音（可选）
                    const audio = new Audio('/static/notification.mp3');
                    audio.play().catch(e => console.log('无法播放提示音'));
                }
                lastCount = data.count;
            });
    }

    // 每分钟检查一次新消息
    setInterval(pollMessages, 60000);
    </script>
    {% endblock %}
</body>
</html> 